package com.zyz.common.core.model;

import com.zyz.common.core.enums.ResultCodeEnum;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Result<T> {

    private int code;

    private String msg;

    private T data;

    public static <T> Result<T> assembleResult(T data, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<T>();
        result.setData(data);
        result.setCode(resultCodeEnum.getCode());
        result.setMsg(resultCodeEnum.getMsg());
        return result;
    }

    public static <T> Result<T> fail(ResultCodeEnum resultCodeEnum) {
        return assembleResult(null, resultCodeEnum);
    }

    public static <T> Result<T> fail() {
        return assembleResult(null,ResultCodeEnum.ERROR);
    }

    public static Result<Void> fail(Integer code, String message) {
        Result<Void> result = new Result<>();
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    public static <T> Result<T> ok () {
        return assembleResult(null,ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok (T data) {
        return assembleResult(data, ResultCodeEnum.SUCCESS);
    }

}
